package com.bfly.mall.admin.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.ActionModel;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.admin.dto.MenuListDTO;
import com.bfly.mall.admin.entity.Role;
import com.bfly.mall.admin.dto.RoleDTO;
import com.bfly.mall.admin.service.IMenuService;
import com.bfly.mall.admin.service.IRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;

/**
 * 系统用户
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/16 13:28
 */
@RestController
@RequestMapping(value = "/manage/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;

    @GetMapping(value = "/all")
    @PermissionCheck(required = false)
    public void listRoleNames(HttpServletResponse response) {
        List<Role> list = roleService.getList();
        List<RoleDTO> roleDTOS = new ArrayList<>();
        getRoles(response, list, roleDTOS);
    }

    /**
     * 角色列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/16 22:02
     */
    @GetMapping(value = "/list")
    public void list(HttpServletResponse response) {
        // 自定义的角色
        Map<String, Object> params = new HashMap<>();
        params.put("is_sys", false);
        List<Role> list = roleService.getList(params);
        List<RoleDTO> roleDTOS = new ArrayList<>();
        getRoles(response, list, roleDTOS);
    }

    private void getRoles(HttpServletResponse response, List<Role> list, List<RoleDTO> roleDTOS) {
        list.forEach(role -> {
            RoleDTO dto = new RoleDTO();
            BeanUtils.copyProperties(role, dto);
            roleDTOS.add(dto);
        });
        ResponseUtil.writeJson(response, ResponseData.getSuccess(roleDTOS));
    }

    /**
     * 新增角色
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/17 22:29
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "新增角色")
    public void add(HttpServletResponse response, @Valid @RequestBody RoleDTO role, BindingResult result) {
        validData(result);
        roleService.save(role);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    @PostMapping(value = "/edit")
    @ActionModel(value = "修改角色")
    public void edit(HttpServletResponse response, @Valid @RequestBody RoleDTO role, BindingResult result) {
        validData(result);
        roleService.edit(role);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    @PostMapping(value = "/delete")
    @ActionModel(value = "删除角色")
    public void delete(HttpServletResponse response, @RequestBody Integer... roleId) {
        roleService.remove(roleId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 获得角色的权限
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/16 11:11
     */
    @GetMapping(value = "/menus")
    @PermissionCheck(required = false)
    public void getPermissions(HttpServletResponse response, @RequestParam("roleId") int roleId) {
        List<MenuListDTO> list = menuService.getFunctionMenuByRole(roleId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(list));
    }

    /**
     * 角色和菜单绑定关系
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/16 11:39
     */
    @PostMapping(value = "/bind")
    @ActionModel(value = "角色分配权限")
    public void bindRoleAndMenu(HttpServletResponse response, @RequestParam("roleId") int roleId, @RequestBody Integer... menus) {
        roleService.bindRoleAndMenu(roleId, menus);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}
